
//  WechatPayManager.m
//  Paoword
//
//  Created by Wensj on 7/1/16.
//  Copyright © 2016 New. All rights reserved.
//

#import "WechatPayManager.h"
#import "NSDictionary+JsonString2Dic.h"
#import "WXApiObject.h"

@interface WechatPayManager()

@end

@implementation WechatPayManager
SYNTHESIZE_SINGLETON_FOR_CLASS(WechatPayManager);

- (instancetype)init {
    if ((self = [super init])) {
        [WXApiManager sharedManager].delegate = self;
    }
    return self;
}


- (void)payWithProduce:(NSString *)produceInfo completionBlock:(MMGPaymentCompletion)completionBlock {
    
    if ([WXApi isWXAppInstalled]) {
        self.completion = completionBlock;
        
        NSDictionary *proInfo = [NSDictionary mmg_dictionaryWithJsonString:produceInfo];
        
        PayReq *req = [[PayReq alloc] init];
        req.openID = (proInfo[@"appid"] && !([proInfo[@"appid"] isKindOfClass:[NSNull class]])) ? proInfo[@"appid"] : @"";
        // 向微信注册，用于微信支付
        [WXApi registerApp:req.openID withDescription:@"prize_claw"];
        
        req.partnerId = (proInfo[@"partnerid"] && !([proInfo[@"partnerid"] isKindOfClass:[NSNull class]])) ? proInfo[@"partnerid"] : @"";
        req.prepayId = (proInfo[@"prepayid"] && !([proInfo[@"prepayid"] isKindOfClass:[NSNull class]])) ? proInfo[@"prepayid"] : 0;
        req.package = (proInfo[@"package"] && !([proInfo[@"package"] isKindOfClass:[NSNull class]])) ? proInfo[@"package"] : @"";//[NSString stringWithFormat:@"prepay_id=%@",req.prepayId];
        req.nonceStr = (proInfo[@"noncestr"] && !([proInfo[@"noncestr"] isKindOfClass:[NSNull class]])) ? proInfo[@"noncestr"] : @"";
        req.timeStamp = (proInfo[@"timestamp"] && !([proInfo[@"timestamp"] isKindOfClass:[NSNull class]])) ? [proInfo[@"timestamp"] intValue] : 0;
        req.sign = (proInfo[@"sign"] && !([proInfo[@"sign"] isKindOfClass:[NSNull class]])) ? proInfo[@"sign"] : @"";
        //订单号
        self.orderNo=(proInfo[@"out_trade_no"] && !([proInfo[@"out_trade_no"] isKindOfClass:[NSNull class]])) ? proInfo[@"out_trade_no"] : @"";
        
        DLog(@"req.package=%@",req.package);
        DLog(@"微信下单结果 : %@", proInfo);
        if (req.prepayId == 0) {
            //下单失败
            if (completionBlock) {
                MMG_PaymentCallBlock_2(completionBlock, @"微信下单失败", NO);
            }
        } else {
            BOOL result = [WXApi sendReq:req];
            DLog(@"微信支付调用 : %d %@",result, proInfo);
        }

    }else {
        //@"您还没安装微信客户端"
        
    }
   
}

#pragma mark - WXApiManagerDelegate
- (void)managerDidRecvPayResponse:(PayResp *)resp {
    NSString *returnMsg = nil;
    BOOL isFinished = NO;
    switch (resp.errCode) {
        case WXSuccess: {
            DLog(@"支付成功－PaySuccess，retcode = %d", resp.errCode);
            
            returnMsg = self.orderNo;
            isFinished = YES;
        }
            break;
        case WXErrCodeCommon: {
            DLog(@"支付:retcode = %d, restr = %@",resp.errCode, resp.errStr);
            returnMsg = @"支付失败";
        }
            break;
        case WXErrCodeSentFail: {
            DLog(@"支付:retcode = %d, restr = %@",resp.errCode, resp.errStr);
            returnMsg = @"支付发送失败";
        }
            break;
        case WXErrCodeAuthDeny: {
            DLog(@"支付:retcode = %d, restr = %@",resp.errCode, resp.errStr);
            returnMsg = @"支付授权失败";
        }
            break;
        case WXErrCodeUnsupport: {
            DLog(@"支付:retcode = %d, restr = %@",resp.errCode, resp.errStr);
            returnMsg = @"您的微信版本不支持微信支付功能";
        }
            break;
        case WXErrCodeUserCancel: {
            DLog(@"支付:retcode = %d, restr = %@",resp.errCode, resp.errStr);
            returnMsg = @"支付取消";
        }
            break;
        default: {
            DLog(@"错误，retcode = %d, retstr = %@", resp.errCode,resp.errStr);
            returnMsg = @"支付失败";
        }
            break;
    }
    
    if (self.completion) {
        MMG_PaymentCallBlock_2(self.completion, returnMsg, isFinished);
    }
}

@end
